package com.meixiang.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.account.SelectMemberStoreEntity;
import com.meixiang.recyclerview.OnItemClick;
import com.meixiang.util.GlideHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 选择店铺
 */
public class SelectMemberStoreAdapter extends RecyclerView.Adapter<SelectMemberStoreAdapter.ViewHolder> {


    private Context context;
    public List<SelectMemberStoreEntity> mList = new ArrayList<>();

    public SelectMemberStoreAdapter(Context context) {
        this.context = context;
    }


    /**
     * 刷新数据
     */
    public void refreshData(List<SelectMemberStoreEntity> list) {
        this.mList.addAll(list);
        this.notifyDataSetChanged();
    }

    public void clearData() {
        this.mList.clear();
        this.notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_item_select_member_store, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    private OnItemClick onItemClickListener;

    public void setOnItemClickListener(OnItemClick onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = holder.getLayoutPosition();
                onItemClickListener.onItemClick(v, pos);

            }
        });

        SelectMemberStoreEntity storeEntity = mList.get(position);
        if (storeEntity != null) {
            GlideHelper.showCircleImage(context, storeEntity.getStoreLogo(), R.mipmap.default_head_portrait, holder.mImgSelectPic);

            if (storeEntity.getGradeName().equals("铜牌会员")) {
                holder.mRlLevel.setBackgroundResource(R.mipmap.tong);
                holder.mTvLevel.setBackgroundResource(R.drawable.text_bronze_background);//
                holder.mTvSelectName.setText(storeEntity.getStoreName());
                holder.mTvLevel.setText(storeEntity.getGradeName());//
            } else if (storeEntity.getGradeName().equals("银牌会员")) {
                holder.mRlLevel.setBackgroundResource(R.mipmap.yin);
                holder.mTvLevel.setBackgroundResource(R.drawable.text_silver_background);//
                holder.mTvSelectName.setText(storeEntity.getStoreName());
                holder.mTvLevel.setText(storeEntity.getGradeName());//
            } else if (storeEntity.getGradeName().equals("金牌会员")) {
                holder.mRlLevel.setBackgroundResource(R.mipmap.jin);
                holder.mTvLevel.setBackgroundResource(R.drawable.text_gold_background);//
                holder.mTvSelectName.setText(storeEntity.getStoreName());
                holder.mTvLevel.setText(storeEntity.getGradeName());//
            }

        }

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.img_select_pic)
        ImageView mImgSelectPic;
        @Bind(R.id.tv_select_name)
        TextView mTvSelectName;
        @Bind(R.id.tv_level)
        TextView mTvLevel;
        @Bind(R.id.rl_level)
        RelativeLayout mRlLevel;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
